// import Vue from 'vue'
// import Router from 'vue-router'
import tender from './tender'
import contractShouzhi from './contractShouzhi'
import revolveMaterialManage from './revolveMaterialManage'
import equip from './equip'
import common from './common'
import OutsourcingartyAdmin from './OutsourcingartyAdmin'
import materialsmanagement from './materialsmanagement'
import base from './base'
import materialSettlement from './materialSettlement'
import projectSettlement from './projectSettlement'
import PCCset from './PCCset'
import settlementModule from './settlementModule'
import { hideLoading } from '@/utils/common'
import store from '@/store'
import landlordContract from './landlordContract'
import projectManagement from './projectManagement'
import comprehensiveReport from './comprehensiveReport' //综合报表
import todo from './todo'
import materialsProcurementPlan from './HouseVersion/materialsProcurementPlan'
import homeVersionContract from './HouseVersion/homeVersionContract'
import houseTender from './HouseVersion/houseTender'
import houseEquipment from './HouseVersion/houseEquipment' //房建版设备

Vue.use(VueRouter)
// TODO: 发布生产版时设为false
Vue.config.devtools = true

// 以下代码解决路由地址重复的报错问题(一劳永逸)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err)
}

const routerInstance = new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/",
      component: () => import("@/pages/index/index"),
      meta: {
        title: "首页",
      },
      redirect: "/demonstration",
    },
    {
      path: "/home",
      component: () => import("@/pages/index/home"),
      meta: {
        title: "客户端首页",
      },
    },
    {
      path: "/login/:user?",
      // 开启props 会把URL中的参数传递给组件
      // 在组件中通过props来接收
      props: true,
      name: "login",
      component: () => import("@/pages/login/login"),
      meta: {
        title: "登录",
      },
    },
    {
      path: "/onlyofficeUpload",
      props: true,
      name: "onlyofficeUpload",
      component: () => import("@/pages/index/onlyofficeUpload"),
      meta: {
        title: "onlyoffice上传模板",
      },
    },

        // 页面
        ...tender,
        ...contractShouzhi,
        ...equip,
        ...common,
        ...OutsourcingartyAdmin,
        ...materialsmanagement,
        ...revolveMaterialManage,
        ...base,
        ...materialSettlement,
        ...projectSettlement,
        ...PCCset,
        ...settlementModule,
        ...landlordContract,
        ...projectManagement,
        ...todo,
        ...materialsProcurementPlan,
        ...homeVersionContract,
        ...houseTender,
        ...houseEquipment,
        ...comprehensiveReport,
    ],
})

const getUser = async (token) => {
    let dispatchRes
    try {
        // 客户端
        if (CefSharp) {
            dispatchRes = await store.dispatch('volideToken', token)
        }
    } catch (e) {
        // web端
        // dispatchRes = await store.dispatch('getToken')
        // 登录过期后再跳转登录界面进行登录
        dispatchRes = await store.dispatch('volideToken', token)
    }
    return dispatchRes
}

// 路由前置导航守卫
// routerInstance.beforeEach(async (to, from, next) => {
//     // alert(getUrlParams('token'))
//     // alert('to: ' + JSON.stringify(to))
//     // alert(store.state.userInfo.token)
//     // 根据路由元信息设置文档标题
//     window.document.title = to.meta.title || 'pcwp 2.0'
//     let { token, orgid, searchOrgId, searchOrgName } = to.query
//     // searchOrgId = 'abc817a46aed-80ca-b149-706d-16e1bc90'
//     // searchOrgName = '四川路桥盛通建筑工程有限公司G4216线宜金高速公路XJ13合同段项目部'
//     if (to.fullPath.includes('/login') || (token && token === store.state.userInfo.token)) {
//         hideLoading()
//         next()
//         return
//     }
//     // 如果没传则从localStorage从取
//     if (!token) {
//         token = localStorage.getItem('token')
//     }
//     // 如果localStorage中也没有，则跳转登录页
//     if (!token) {
//         // 招标报名放行
//         if (to.path.includes('openBiddingRegAdd') || to.path.includes('OpenBiddingRegAdd')) {
//             next()
//             return
//         }
//         hideLoading()
//         // if (
//         //     window.location.href.includes('OpenBiddingRegAdd') ||
//         //     window.location.href.includes('openBiddingRegAdd')
//         // ) {
//         //     next()
//         //     return
//         // }
//         next('/login')
//         return
//     }
//     // 将获取到的orgId保存到本地
//     if (orgid) {
//         localStorage.setItem('orgId', orgid)
//     }
//     const keys = Object.keys(to.query)
//     if(keys.includes('searchOrgId')) {
//         if(searchOrgId) {
//             localStorage.setItem('searchOrgId', searchOrgId)
//         }else{
//             localStorage.setItem('searchOrgId', '')
//         }
//     }
//     if(keys.includes('searchOrgName')) {
//         if(searchOrgId) {
//             localStorage.setItem('searchOrgName', searchOrgName)
//         }else{
//             localStorage.setItem('searchOrgName', '')
//         }
//     }
//     // 获取token、userName、userNumber存入localStorage
//     await getUser(token)
//     // 获取用户信息存入vuex
//     // 获取公共字典存入vuex
//     const res = await Promise.all([
//         store.dispatch('userIdGetOrgInfo'),
//         store.dispatch('getCurrency'),
//         store.dispatch('equip/setCurrencyForm'),
//         store.dispatch('getMenuId')
//     ])
//     const getNetwork = res.find(value => value !== undefined)
//     const toPath = to.path
//     const noGetBaseCyByOrgListurl = store.state.noGetBaseCyByOrgListurl
//     if (!noGetBaseCyByOrgListurl.includes(toPath)) {
//         store.dispatch('getBaseCyByOrgId')
//     }
//     if (getNetwork) {
//         setTimeout(() => {
//             hideLoading()
//             next()
//         }, 100)
//     } else {
//         hideLoading()
//         next()
//     }
// })

// 切换路由滚动条恢复到顶部
// eslint-disable-next-line no-unused-vars
routerInstance.afterEach((to, from, next) => {
    window.scrollTo(0, 0)
})

export default routerInstance
